<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.css" rel="stylesheet">
  <style>
    .todo-container{
      max-width: 600px;
      border: 1px solid #333;
      padding: 20px;
      border-radius: 6px;
      margin: 20px auto;
    }
    .ani-enter{
      transform: translateX(-100%);
      opacity: 0;
    }
    .ani-leave-to{
      transform: translateX(100%);
      opacity: 0;
    }
    .ani-enter-active,.ani-leave-active{
      transition: all 500ms;
    }
  </style>
</head>
<body>
  <div id="app">
    <div class="todo-container">
      <!-- todo头部 -->
      <div class="todo-header row">
        <div class="col-md-10">
          <input type="text" v-model.trim="inputTxt" class="form-control">
        </div>
        <div class="col-md-2">
          <button @click="addTodo" class="btn btn-primary">增加</button>
        </div>
      </div>
      <!-- /todo头部 -->
      <!-- todoContent -->
      <ul class="list-group" style="margin-top: 20px">
        <li
          v-if="todos.length === 0"
          class="list-group-item text-center">
          暂无待办事项
        </li>
        <transition-group name="ani">
        <li 
          v-for="(todo,index) in todos"
          :key="index"
          class="list-group-item">
          <span>
            {{todo.content}}
          </span>
          <div class="btns pull-right">
            <button 
              @click="delItem(index)"
              class="btn btn-danger btn-xs">删除</button>
            <button 
              class="btn btn-success btn-xs" 
              @click="setTodoFinished(index)"
              :disabled="todo.finished">
              {{todo.finished?'已完成':'点击完成'}}
            </button>
          </div>
        </li>
      </transition-group>
      </ul>
      <!-- /todoContent -->
    </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        inputTxt: '', // 输入框绑定输入内容
        todos: [ // 待办事项列表
          {
            content: '明天出去活动',
            finished: false
          },
          {
            content: '准备出去轰趴的吃的',
            finished: true
          }
        ]
      },
      methods: {
        setTodoFinished(index){
          // 设置代办事项已完成
          if(confirm('您确认完成了吗?')){
            this.todos[index].finished = true;
          }
        },
        delItem(index){
          // 删除待办事项
          if(confirm('您确认删除嘛?')){
            this.todos.splice(index, 1);
          }
        },
        addTodo(){
          if(this.inputTxt ===''){
            return false;
          }
          // 新增待办事项
          this.todos.push({
            content: this.inputTxt,
            finished: false
          })
          this.inputTxt = '';
        },
      }
    })
  </script>
</body>
</html>